//
//  RecommentCollectionViewCell.m
//  Cars' home
//
//  Created by dapeng on 15/11/27.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "RecommentCollectionViewCell.h"

#import "Theme-PrefixHeader.pch"
@implementation RecommentCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self tableViews];
        [self addSubview:self.tableView];
        [_tableView release];
        self.count = 1;

        [self header];
        [self footer];
        [self.tableView headerBeginRefreshing];
        [self changeColor];
    }return self;
}

- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.tableView.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        self.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView registerClass:[customTableViewCell class] forCellReuseIdentifier:@"customTableViewCellIndentifier"];
    [_tableView registerClass:[SAPThreePicTableViewCell class] forCellReuseIdentifier:@"SAPThreePicTableViewCellIndentifier"];
    
    
}
- (void)header {
    _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [_tableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SAPNetWorkTool getWithUrl:newest parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                self.oneModel = [[recommentOneModel alloc] initWithDataSource:dic];
                _HUD.hidden = YES;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
                [_tableView headerEndRefreshing];
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
        });
    }];
}
- (void)footer {
    [_tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *str = [[NSString alloc] init];
            for (NSDictionary *dic in self.oneModel.newslistArray) {
                str = [dic valueForKey:@"lasttime"];
            }
            NSString *string = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/news/newslist-pm1-c0-nt0-p2-s30-l%@.json", str];
            [SAPNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
                NSDictionary *dic = [result valueForKey:@"result"];
                recommentOneModel *oneModel = [[recommentOneModel alloc] initWithDataSource:dic];
                [self.oneModel.newslistArray addObjectsFromArray:oneModel.newslistArray];
                [_tableView reloadData];
                [_tableView footerEndRefreshing];
            } fail:^(NSError *error) {
                NSLog(@"%@", error);
            }];
            
            });
        
    }];
                       
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 69);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    customTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customTableViewCellIndentifier"];

    if (self.oneModel.newslistArray) {
        
    recommentTwoModel *twoModel = self.oneModel.newslistArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@", twoModel.mediatype];
    
    if ([str isEqualToString:@"6"] || [str isEqualToString:@"10"]) {
        SAPThreePicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPThreePicTableViewCellIndentifier"];
            cell.twoModel2 = twoModel;
        return cell;
    }
            cell.twoModel = twoModel;
    }
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSMutableArray *pictureArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (recommentTwoModel *twoModel in self.oneModel.focusimgArray) {
        [pictureArray addObject:twoModel.imgurl];
        [titleArray addObject:twoModel.title];
       }
    Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, SCREEN_SIZE.height / 3.5)];
    carousel.delegate = self;
   

    [carousel setArray:[NSArray arrayWithArray:pictureArray] withTitArray:titleArray];
    return carousel;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    recommentTwoModel *twoModel = self.oneModel.newslistArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.2.5/content/News/newscontent-a2-pm1-v4.2.5-n%@-lz0-sp0-nt0-sa1-p0-c1-fs0-cw320.html", twoModel.id];
        NSURL *url = [NSURL URLWithString:str];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 100)];
        webView.backgroundColor = [UIColor redColor];
        [webView loadRequest:request];
        webView.scalesPageToFit = YES;
        webView.delegate = self;
        [self addSubview:webView];
    
    [self.delegate passValue:webView withUrl:str];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.oneModel.newslistArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   
    return SCREEN_SIZE.height / 3.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    recommentTwoModel *two = self.oneModel.newslistArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%@", two.mediatype];
    if ([str isEqualToString:@"6"] || [str isEqualToString:@"10"]) {
        return 140;
    }
    return 90;
}

- (void)passCarouselValues:(NSInteger)count {
    recommentTwoModel *twoModel = self.oneModel.focusimgArray[count];
    NSString *str = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=1&ourl=http://cont.app.autohome.com.cn/autov5.0.5/content/News/newssharedcontent-n%@.html", twoModel.id];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    webView.backgroundColor = [UIColor redColor];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    webView.delegate = self;
//    [self addSubview:webView];
    
    [self.delegate passValue:webView withUrl:str];
}

@end
